import { getArticlelist, getArticleContent } from '@/api/article'

const article = {
    state: {
        type: 1,
        posts: []
    },
    mutations: {
        SET_POSTS (state, posts) {
            state.posts = state.posts.concat(posts)
        }
    },
    actions: {
        getArticlelist ({ commit, state }, payload) {
            return new Promise((resolve, reject) => {
                getArticlelist(state.type).then(response => {
                    const data = response.data
                    commit('SET_POSTS', data.posts)
                    resolve()
                }).catch(err => {
                    reject(err)
                })
            })
        },
        getArticleContent ({ commit }, id) {
            return new Promise((resolve, reject) => {
                getArticleContent(id).then(response => {
                    const data = response.data
                    resolve(data.posts)
                }).catch(err => {
                    reject(err)
                })
            })
        }
    }
}

export default article